import "./index.less";
import {Fetch} from  '../../libs/';
import {Url, md5Url} from  '../../libs/Url';
import {connect} from 'react-redux';
import {fetch} from  '../../stores/action';
import {parseSearch} from  '../../libs/utils';
import {Scrollbars} from 'react-custom-scrollbars'
import CSS from '../../libs/CSS'

class News extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            active: 1,
            photo: 1,
        }
    }

    componentDidMount() {
        const {dispatch} = this.props;
        let clientInfo = parseSearch();
        if (clientInfo && clientInfo.clientid) {
            Fetch(Url.GET_CLASS, {body: {deviceid: clientInfo.clientid}})
                .then((res)=> {
                    if (res['class'] && res['class'].classid) {
                        clientInfo['class'] = res['class'];
                        clientInfo['school'] = res['school'];
                        clientInfo['device'] = res['device'];
                        clientInfo['classroom'] = res['classroom'];
                        // dispatch(clientInfoSet(clientInfo));
                        dispatch(fetch({url: Url.NEWS, classid: res['class'].classid}))
                    } else {
                        alert('设备码没有跟班级对应，请核验!')
                    }
                })
                .catch(e=> {
                    console.log(e)
                })
        } else {
            alert('设备码不合法，请核验!')
        }
    }

    _textLimit(str = '', number) {
        if(str.length > number) {
            return str.substring(0, number) + '...';
        }
        return str;
    }

    render() {
        let data = this.props[md5Url.NEWS];
        data = data.news ? data : {news: [{}]};
        const item_ = data.news.length ? data.news[0] : {};
        return (
            <div className="news clearfix" style={{}}>
                <div className="panel-item panel1 container-border">
                    {!item_.fileids ? null :
                        <div
                            style = {{
                                padding: 0,
                                width: CSS.pixel(452),
                                height: CSS.pixel(339),
                                backgroundImage: CSS.parseBackgroundImage(Url.FUN_UPLOAD_PREVIEW(item_.fileids)),
                                backgroundSize: '100% 100%',
                                backgroundRepeat: 'no-repeat'
                            }}
                        />
                    }
                    <div style = {{flex: 1}}>
                        <div className="title">
                            {item_.title}
                        </div>
                        <p style = {{wordBreak: 'break-all'}}>
                            {this._textLimit(item_.summary, 60)}
                        </p>
                    </div>
                </div>
                <div className="panel-item panel2 container-border">
                    <div className="news-board"
                         style={{
                             backgroundSize: '100% 100%',
                             height: (window.innerHeight - 45 - 30 - 40 * 2 - 180 - 120 - 15)
                         }}>
                        {
                            data.news.slice(1, 4).map((item, index)=> {
                                return <div key={index} className="item">
                                    <div>
                                        <div>
                                            <span className="category">{item.classify ? item.classify.name : ''}
                                                ：</span>
                                            {item.title}
                                        </div>
                                        <p>{CSS.textLimit(item.summary, 80)}
                                        </p>
                                    </div>
                                    {!item.fileids ? null :
                                        <img src={Url.FUN_UPLOAD_PREVIEW(item.fileids)} alt="" style = {{width: CSS.pixel(200), height: CSS.pixel(140)}} />
                                    }
                                </div>
                            })
                        }
                    </div>
                </div>
            </div>
        )
    }
}

const select = state => {
    return {
        [md5Url.NEWS]: state.fetchBySubject[md5Url.NEWS],
        clientInfo: state.clientInfo
    }
};

export default connect(select)(News)